package com.didispace.chapter310.service;

import com.didispace.chapter310.Test2;
import com.didispace.chapter310.User;
import com.didispace.chapter310.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

@Service
@Slf4j
public class UserService {

  @Autowired
  private UserRepository userRepository;

  @Autowired
  private Test test;

  @Autowired
  private Test2 test2;

  @Transactional
  public void test1() {
    userRepository.save(new User("AA1", 10));
    //回滚
    throw new RuntimeException();
  }

  @Transactional
  public void test2() {
    userRepository.save(new User("AA2", 10));
    //不回滚
    try {
      throw new RuntimeException();
    } catch (Exception e) {
      log.error("异常捕获：", e);
      throw new RuntimeException("");
    }
  }

  @Transactional
  public void test3() {
    userRepository.save(new User("AA3", 10));
    //异常回滚
    userRepository.save(new User("1234567890", 20));
  }

  @Transactional
  public void test4() {
    userRepository.save(new User("AA4", 10));
    //异常回滚
    try {
      userRepository.save(new User("123456sssssssssssssssssssssssssssss7890", 20));
    } catch (ConstraintViolationException e) {
      log.error("异常捕获：", e);
    }
  }



  public void test5() {
    test6();
  }

  @Transactional
  public void test6(){
    User a = new User("A", 100);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }


  @Transactional
  public void test7() {
    test8();
  }

  public void test8(){
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }


  @Transactional
  public void test9() {
    this.test10();
  }

  public void test10(){
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }

  @Transactional
   void test11() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }

  ///rollbackFor 这个是指定 非 RuntimeException和Error的异常
  @Transactional(rollbackFor = NumberFormatException.class)
  public void test12() throws FileNotFoundException {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    int i=1/0;
  }

  @Transactional
  public void test135() throws FileNotFoundException {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    FileInputStream file =new FileInputStream("d:/aa.y");
  }



  //只读
  @Transactional(rollbackFor = RuntimeException.class,readOnly = true)
  public void test13() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    //throw new RuntimeException("test");
  }

  @Transactional(rollbackFor = RuntimeException.class,readOnly = true,value="transactionManager")
  public void test14() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    //throw new RuntimeException("test");
  }


  //无事务再跑
  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.NOT_SUPPORTED)
  public void test15() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }

  //新事物
  @Transactional(propagation = Propagation.REQUIRED)
  public void test16() {
    test2.test21();
      test.test2();


  }

  @Transactional(propagation = Propagation.REQUIRED)
  public void test165() {
    test2.test21();
      test.test3();
  }

  @Transactional(propagation = Propagation.REQUIRED)
  public void test1655() {
    test2.test22();
    test.test3();
  }
  @Transactional(propagation = Propagation.REQUIRED)
  public void test16555() {
    test2.test22();
    test.test4();
  }


  @Transactional(propagation = Propagation.REQUIRED)
  public void test165555() {
    test2.test21();
    test.test4();
  }


  @Transactional(propagation = Propagation.REQUIRED)
  public void test1655555() {
    A();
    HHHH222222();

  }

  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.NEVER)
  public void A() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    /// throw new RuntimeException("test");
  }


  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.REQUIRES_NEW)
  public void HHHH222222() {
    User a = new User("fasd", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw  new RuntimeException("'");
  }

  //必须有事务 没有的话 异常
  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager")
  public void test17() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    test18();
  }

  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.MANDATORY)
  public void test18(){
    userRepository.save(new User("1234567890", 20));
    throw new RuntimeException("test");
  }

  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.NOT_SUPPORTED)
  public void test19() {
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    test20();
  }

  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.MANDATORY)
  public void test20(){
    userRepository.save(new User("1234567890", 20));
    throw new RuntimeException("test");
  }



  @Transactional
  public void test21() {
    try {
      test22();
    }catch (Exception e){
      e.printStackTrace();
    }

  }
  @Transactional(rollbackFor = RuntimeException.class,readOnly = false,value="transactionManager",propagation= Propagation.MANDATORY)
  public void test22(){
    User a = new User("A", 10);
    System.out.println(a.getName());
    System.out.println(a.getName().length());
    userRepository.save(a);
    throw new RuntimeException("test");
  }


  public String register() {
    return "";
  }
}
